package com.dhcc.bpm.modules.sandbox.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.json.JSONObject;
import com.dhcc.bpm.modules.sandbox.entity.OpcSandboxCodeExpress;
import com.dhcc.bpm.modules.sandbox.entity.ReturnCode;
import com.dhcc.bpm.modules.sandbox.service.IOpcSandboxCodeExpressService;
import com.dhcc.bpm.modules.sandbox.service.IReturnCodeService;
import com.dhcc.bpm.modules.sandbox.vo.OpcSandboxCodeExpressVO;
import com.dhcc.bpm.modules.system.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OpcSandboxCodeExpressController
 * @Description 返回码达式表 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping("bpm-sandbox/opcsandboxcodeexpress")
@Api(value = "返回码达式表", tags = "返回码达式表接口")
public class OpcSandboxCodeExpressController extends BladeController {

	private IDictService dictService;

	private IReturnCodeService returnCodeService;

	private IOpcSandboxCodeExpressService opcSandboxCodeExpressService;


	/**
	 * @Description 详情
	 * @Author sjm
	 * @Date 21:27 2020/2/21 0021
	 * @param opcSandboxCodeExpress
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.sandbox.entity.OpcSandboxCodeExpress>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入opcSandboxCodeExpress")
	@ApiLog("返回码达式详情")
	@Log(title = "返回码达式详情", businessType = BusinessType.SELECT)
	public R<OpcSandboxCodeExpress> detail(OpcSandboxCodeExpress opcSandboxCodeExpress) {
		OpcSandboxCodeExpress detail = opcSandboxCodeExpressService.getOne(Condition.getQueryWrapper(opcSandboxCodeExpress));
		return R.data(detail);
	}

	/**
	 * @Description 分页 返回码达式表
	 * @Author sjm
	 * @Date 21:27 2020/2/21 0021
	 * @param opcSandboxCodeExpress
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.entity.OpcSandboxCodeExpress>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入opcSandboxCodeExpress")
	@ApiLog("分页查询返回码达式表")
	@Log(title = "查询返回码达式表", businessType = BusinessType.SELECT)
	public R<IPage<OpcSandboxCodeExpress>> list(@ApiIgnore @RequestParam Map<String, Object> opcSandboxCodeExpress, Query query) {
		IPage<OpcSandboxCodeExpress> pages = opcSandboxCodeExpressService.page(Condition.getPage(query), Condition.getQueryWrapper(opcSandboxCodeExpress, OpcSandboxCodeExpress.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 返回码达式表
	 * @Author sjm
	 * @Date 21:27 2020/2/21 0021
	 * @param opcSandboxCodeExpress
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.vo.OpcSandboxCodeExpressVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入opcSandboxCodeExpress")
	@ApiLog("分页查询返回码达式表")
	@Log(title = "查询返回码达式表", businessType = BusinessType.SELECT)
	public R<IPage<OpcSandboxCodeExpressVO>> page(OpcSandboxCodeExpressVO opcSandboxCodeExpress, Query query) {
		IPage<OpcSandboxCodeExpressVO> pages = opcSandboxCodeExpressService.selectOpcSandboxCodeExpressPage(Condition.getPage(query), opcSandboxCodeExpress);
		return R.data(pages);
	}

	/**
	 * @Description 新增 返回码达式表
	 * @Author sjm
	 * @Date 21:28 2020/2/21 0021
	 * @param opcSandboxCodeExpress
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入opcSandboxCodeExpress")
	@ApiLog("新增返回码达式表")
	@Log(title = "新增返回码达式表", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody OpcSandboxCodeExpress opcSandboxCodeExpress) {
		return R.status(opcSandboxCodeExpressService.save(opcSandboxCodeExpress));
	}

	/**
	 * @Description 修改 返回码达式表
	 * @Author sjm
	 * @Date 21:28 2020/2/21 0021
	 * @param opcSandboxCodeExpress
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入opcSandboxCodeExpress")
	@ApiLog("修改返回码达式表")
	@Log(title = "修改返回码达式表", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody OpcSandboxCodeExpress opcSandboxCodeExpress) {
		return R.status(opcSandboxCodeExpressService.updateById(opcSandboxCodeExpress));
	}

	/**
	 * @Description 新增或修改 返回码达式表
	 * @Author sjm
	 * @Date 21:28 2020/2/21 0021
	 * @param opcSandboxCodeExpress
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入opcSandboxCodeExpress")
	@ApiLog("新增或修改返回码达式表")
	@Log(title = "新增或修改返回码达式表", businessType = BusinessType.UPDATE)
	public R submit(@Valid @RequestBody OpcSandboxCodeExpressVO opcSandboxCodeExpress) {
		long id = opcSandboxCodeExpress.getId() == null ? -1 : opcSandboxCodeExpress.getId();
		String leftOper = "1";
		if(id < 0){
			opcSandboxCodeExpress.setIsDeleted(0);
		}
		ReturnCode returnCode = returnCodeService.getById(opcSandboxCodeExpress.getCodeId());
		int expLen = 0;
		if(returnCode.getReturnCodeExp() != null){
			expLen = returnCode.getReturnCodeExp().length();
		}
		if(leftOper.equals(opcSandboxCodeExpress.getLeftOper())){
			opcSandboxCodeExpress.setLeftOper("");
		}
		if(leftOper.equals(opcSandboxCodeExpress.getRightOper())){
			opcSandboxCodeExpress.setRightOper("");
		}
		boolean bool = opcSandboxCodeExpressService.saveOrUpdate(opcSandboxCodeExpress);
		if(!bool){
			R.status(false);
		}
		String express = sandboxCodeExpressUtil(opcSandboxCodeExpress);
		buildExpress(opcSandboxCodeExpress.getCodeId());
		JSONObject json = new JSONObject();
		if (id > 0){
			String paramExpressRight = "";
			String paramExpress = "";
			if(!"".equals(express)) {
				ReturnCode returnCodes = returnCodeService.getById(opcSandboxCodeExpress.getCodeId());
				paramExpress = returnCodes.getReturnCodeExp();
				int len = paramExpress.indexOf(express) == -1 ? -2 : paramExpress.indexOf(express);
				paramExpressRight = paramExpress.substring(len + express.length(), paramExpress.length());
				String expressLogic = opcSandboxCodeExpress.getExpressLogic();
				if(len > 0 ) {
					paramExpress = paramExpress.substring(0, len);
				}else{
					paramExpress = "";
					express = express.substring(2, express.length());
				}
			}
			json.put("leftExp", paramExpress);
			json.put("centerExp", express);
			json.put("rightExp", paramExpressRight);
		}else{
			if(expLen < 1){
				express = express.substring(2, express.length());
			}
			json.put("leftExp", returnCode.getReturnCodeExp());
			json.put("centerExp", express);
			json.put("rightExp", "");
		}

		return R.data(json);
	}

	/**
	 * @Description 删除 返回码达式表
	 * @Author sjm
	 * @Date 21:28 2020/2/21 0021
	 * @param ids
	 * @param codeId
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "删除", notes = "传入ids")
	@ApiLog("删除返回码达式表")
	@Log(title = "删除返回码达式表", businessType = BusinessType.INSERT)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids,
					@ApiParam(value = "主键集合", required = true) @RequestParam String codeId) {
		 boolean bool = opcSandboxCodeExpressService.removeByIds(Func.toLongList(ids));
		 if(bool){
			 buildExpress(codeId);
		 }
		return  R.status(bool);
	}

	/**
	 * @Description 生成表达式
	 * @Author sjm
	 * @Date 21:28 2020/2/21 0021
	 * @param codeId
	 * @return org.springblade.core.tool.api.R
	 **/
	@GetMapping("/buildExpress")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "生成表达式", notes = "传入codeId")
	@ApiLog("生成返回码表达式")
	@Log(title = "生成返回码表达式", businessType = BusinessType.INSERT)
	public R buildExpress(@ApiParam(value = "主键集合", required = true) @RequestParam String codeId)
	{
		String express = "";
		String expression = "";
		List<OpcSandboxCodeExpress> list = opcSandboxCodeExpressService.selectByCodeId(codeId);
		if(list != null && list.size() > 0) {
			for(OpcSandboxCodeExpress code : list) {
				express += sandboxCodeExpressUtil(code);
			}
			expression = express.substring(2, express.length());
		}
		ReturnCode returnCode = new ReturnCode();
		returnCode.setReturnCodeExp(expression);
		returnCode.setId(Long.valueOf(codeId));
		returnCodeService.updateById(returnCode);
		return R.data(expression);

	}

	public String sandboxCodeExpressUtil(OpcSandboxCodeExpress sandboxCodeExpress) {
		String express = "";
		String exp = "0";
		String leftOper = "2";
		String rightOper = "3";
		String name = sandboxCodeExpress.getName();
		if(sandboxCodeExpress.getExpressLogic() != null){
			if(exp.equals(sandboxCodeExpress.getExpressLogic())){
				express += "&&";
			}else{
				express += "||";
			}
		}
		if(leftOper.equals(sandboxCodeExpress.getLeftOper())) {
			express += "(";
		}
		express += name;
		express += expressOper(sandboxCodeExpress.getExpressOper());
		express += "'" + sandboxCodeExpress.getExpressLen() + "'";
		if(rightOper.equals(sandboxCodeExpress.getRightOper())){
			express += ")";
		}
		return express;
	}

	public String expressOper(String expressOper){
		String result = "";
		switch (Integer.valueOf(expressOper)){
			case 1:
				result = "==";
				break;
			case 2:
				result = "<";
				break;
			case 3:
				result = ">";
				break;
			case 4:
				result = "!=";
				break;
			case 5:
				result = "<=";
				break;
			case 6:
				result = ">=";
				break;
			default:
				break;
		}
		return result;
	}
}
